package edu.ncst.baseweb.service.impl.webmanage;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.web.LabPlatform;
import edu.ncst.baseweb.service.intrer.webmanage.LabPlatformManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class LabPlatformManageImpl implements LabPlatformManageService {

    @Autowired private edu.ncst.baseweb.mapper.LabPlatformMapper LabPlatformMapper;


    @Override
    public Result<IPage<LabPlatform>> list(LabPlatform LabPlatform) {
        QueryWrapper<LabPlatform> queryWrapper = Wrappers.query();


        Page<LabPlatform> LabPlatformPage = new Page<>(LabPlatform.getCurrent(), LabPlatform.getPageSize());
        IPage<LabPlatform> LabPlatformIPage = LabPlatformMapper.selectPage(LabPlatformPage, queryWrapper);

        return Result.ok(LabPlatformIPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> save(LabPlatform LabPlatform) {
        if (LabPlatform.getId() != null) {
            //修改
            LabPlatformMapper.updateById(LabPlatform);

        } else {
            //新增
            LabPlatformMapper.insert(LabPlatform);
        }

        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> delete(String LabPlatformId) throws Exception {

        if (StringUtils.isBlank(LabPlatformId)) {
            throw new Exception("被删除的id为空");
        }

        List<String> ids = Arrays.stream(LabPlatformId.split(",")).collect(Collectors.toList());

        // 删除  id 在 ids 集合中的记录
        LabPlatformMapper.deleteBatchIds(ids);

        return Result.failure(200, "删除成功！");
    }
    //上传图片和视频
    @Override
    public String uploadFile(MultipartFile file, Integer id){
        try {
            String fileExtension = getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));

            // 定义不同类型的文件扩展名
            List<String> imageExtensions = Arrays.asList("jpg", "jpeg", "png", "gif");

            if (imageExtensions.contains(fileExtension)){
                String basePath = System.getProperty("user.dir");
                String imagePath = basePath + "/files/bigEqu/" + file.getOriginalFilename();
                file.transferTo(new File(imagePath));
                LabPlatformMapper.updateImageById(id,file.getOriginalFilename(),imagePath);

                return "图片上传成功";
            }
            return "上传成功";
        } catch (Exception e) {
            return "上传失败: " + e.getMessage();
        }
    }
    //获取文件名的方法
    private String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex + 1).toLowerCase();
        }
        return "";
    }
}
